const { think } = require('thinkjs');
const Base = require('./base.js');
const moment = require('moment');

module.exports = class extends Base {
  async holidayAction() {
    const list = await this.service('menjin').holiday();
    return this.success(list);
  }
  async openAction() {
    const list = await this.service('menjin').open('0');
    return this.success(list);
  }
  async callbackAction() {
    const data = this.post();
    console.log(data, 123123);
    return this.success();
  }

  async shatdownAction() {
    const userList = await this.service('menjin').getUser();
    const userInfo = [];
    for (const item of userList) {
      if (item.userName === '陈珊珊') {
        userInfo.push({
          pin: item.pin,
          // name: item.userName,
          // deptId: item.deptId,
          startTime: moment()
            .subtract(1, 'year')
            .format('YYYY-MM-DD HH:mm:ss'),
          endTime: moment()
            .subtract(1, 'hour')
            .format('YYYY-MM-DD HH:mm:ss'),
        });
      }
    }
    console.log(userInfo, '请求');
    await this.service('menjin').shatdown(userInfo);
    return this.success();
  }
  async pushAction() {
    const data = this.post();

    const type = {
      '0': '正常刷卡开门',
      '3': '密码开门',
      // 10: '多卡刷卡',
      '18': '超级密码开门',
      '19': '人脸比对成功',
    };

    const types = ['0', '3', '18', '19'];

    if (data.Data && data.Data.length) {
      data.Data.forEach(async (item) => {
        if (types.includes(item.EventType)) {
          console.log(item, 'type____');
          if (!think.isEmpty(item.Pin)) {
            // const cardNum = item.CardNum;
            // const name = item.Name;
            // const department = item.Department;
            // const direction = item.Direction;
            // const recordIndex = item.RecordIndex;
            const hasUser = await this.model('menjin_user')
              .where({
                pin: item.Pin,
                sn: item.SN,
              })
              .find();
            if (think.isEmpty(hasUser)) {
              this.model('menjin_user').add({
                pin: item.Pin,
                sn: item.SN,
                create_time: ['exp', 'CURRENT_TIMESTAMP()'],
              });
            } else {
              this.model('menjin_user').where({
                id: hasUser.id,
                update_time: ['exp', 'CURRENT_TIMESTAMP()'],
              });
            }
          }
        }
      });
    }

    // Data: [
    //          {
    //            CardID: '',
    //            CardNum: '',
    //            CropImage: '',
    //            DateTime: '2025-03-19 15:16:40',
    //            Department: '',
    //            DeviceSn: '2509800',
    //            Direction: '1',
    //            EventType: '1',
    //            EventTypeName: '远程开门 ',
    //            InputAddr: '1',
    //            InputType: '0',
    //            Name: '',
    //            OutputAddr: '1',
    //            OutputType: '1',
    //            Pin: '',
    //            RecordIndex: '454554',
    //            SN: '2509800'
    //          }
    //        ],
    //        CMD: 'venus_event',
    //        SN: '2509800'
    //      }
    console.log(data, 'pushLogs____');
    return this.success();
  }
};
